<?php
namespace App\Repos;
use App\Models\User;
use App\Models\UserCoordinate;
use App\Models\UserAddress;
use App\Models\UserEnvelope;
use App\Models\Order;
use App\Models\UserMoneyLog;
use App\Models\UserShopMoney;
use App\Models\UserCoupon;
use DB,Log;
use App\Services\RedisService;

class UserRepo
{
	public static function  getList($where=[],$num=10)
	{	   
	  	$query = User::query();
	  	if(array_get($where,'keyword')) {
	  		$query->where('name','like','%'.$where['keyword'].'%');
	  		$query->orWhere('phone','like','%'.$where['keyword'].'%');
	  	}
	  	$list = $query->paginate($num); 
	  	return $list;     
	}

	public static function create($data)
	{
		$coord['lat'] = $data['longitude'];
		$coord['lng'] = $data['latitude'];
		$data['token'] = md5($data['small_openid']);
		$data['token_date'] = date('Y-m-d H:i:s',strtotime("+1 week"));
		// array_forget($data,'longitude');
		// array_forget($data,'latitude');
		$flag = '';
		DB::beginTransaction();
		try {
			$where['small_openid'] = $data['small_openid'];
			$user = User::updateOrCreate($where,$data);

			//$coord['user_id'] = $user->id;
			// 记录经纬度		
			//$flag = UserCoordinate::create($coord);

			
			DB::commit();
		} catch (\Exception $e) {

			Log::info('获取token出错：'.$e->getMessage());
			DB::rollBack();
			return false;
		}
		
		return $user;
	}

	public static function find($where=[])
	{
		return User::where($where)->first();
	}

	public static function  getCoordinate($user_id,$num=10)
	{	   
	  	$list = UserCoordinate::where(['user_id'=>$user_id])->paginate($num); 
	  	return $list;     
	}

	public static function  getAddress($user_id,$num=10)
	{	   
	  	$list = UserAddress::where(['user_id'=>$user_id])
	  						->orderBy('is_default','desc')
	  						->paginate($num); 
	  	return $list;     
	}

	public static function  getEnvelope($user_id,$num=10)
	{	   
	  	$list = UserEnvelope::with(['user','shopEnvelope.shop'])->where(['user_id'=>$user_id])->paginate($num); 
	  	return $list;     
	}

	public static function getUserMoneyLog($where=[],$num=10)
	{
		
		$list = UserMoneyLog::where($where)
							->paginate($num);
							
		return $list;					
	}

	public static function getUserCoupon($user_id,$num=10)
	{
		$where['user_id'] = $user_id;
		//$where['type'] =1;
		$list = UserCoupon::with(['user','shop'])
							->where($where)
							->paginate($num);
		return $list;
	}

	public static function  getOrders($user_id,$num=10)
	{	   
	  	$list = Order::where(['user_id'=>$user_id])->paginate($num); 
	  	return $list;     
	}


	public static function update($where,$data)
	{
		$rs['status'] = false;
		$flag = User::where($where)->update($data);

		if($flag) {
			$rs['status'] = true;
			return $rs;
		}
		return $rs;
	}

	public static function changeStatus($id,$data)
	{
		$updateData['status'] = $data['status'];
        self::update(['id'=>$id],$updateData);
		$flag = AdminHandlerLogRepo::create('User',$id,$data);
		return true;
	}


	/**
	 * 统计页面用
	 */
	public static function getUsers($where=[],$num=10)
	{	   
	  	$list = User::where($where)->paginate($num); 
	  	return $list;     
	}

	public static function count()
	{
		$num = User::count();
		return $num;
	}

	public static function getUserShopMoney($where)
	{
		$money = 0;
		$data  = UserShopMoney::where($where)->first();
		if(object_get($data,'money')) {
			return $data->money;
		}
		return $money;
	} 
	
    public static function findByOpenid($openid)
    {
        return User::where(['openid'=>$openid])->first();
    }

	
	
	
}